package com.xiaoxing.welcome.mvp.ui;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.xiaoxing.welcome.R;
import com.xiaoxing.welcome.di.component.DaggerWelcomeComponent;
import com.xiaoxing.welcome.di.module.WelcomeModule;
import com.xiaoxing.welcome.mvp.contract.WelcomeContract;
import com.xiaoxing.welcome.mvp.model.entity.HomepageInforms;
import com.xiaoxing.welcome.mvp.presenter.WelcomePresenter;

import java.util.Random;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import me.jessyan.armscomponent.commonsdk.utils.Utils;


@Route(path = RouterHub.WELCOME_WELCOMEACTIVITY)
public class WelcomeActivity extends BaseActivity<WelcomePresenter> implements WelcomeContract.View, MainPopup.MessageOk {

    private static final int ANIM_TIME = 2000;
    private static final float SCALE_END = 1.15F;
    private static final int[] Imgs = {
            R.drawable.guide_img};
    ImageView mIVEntry;


    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerWelcomeComponent
                .builder()
                .appComponent(appComponent)
                .welcomeModule(new WelcomeModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_welcome;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {


//        mIVEntry = findViewById(R.id.iv_entry);

//        boolean isFirstOpen = DataHelper.getBoolean(this, AppConstants.FIRST_OPEN, true);
//        if (!mSharedPreferencesHelper.getBoolean(BaseConstants.IS_THE_FIRST)) {
//
//            Intent intent = new Intent(WelcomeActivity.this, GuideActivity.class);
//            startActivity(intent);
//            finish();
//            return;
//
//        }

//        mPresenter.homepageInforms();
        startMainActivity();
    }

    @SuppressLint("CheckResult")
    private void startMainActivity() {
        Random random = new Random(SystemClock.elapsedRealtime());
        mIVEntry.setImageResource(Imgs[random.nextInt(Imgs.length)]);


        Observable.timer(2000, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        Utils.navigation(WelcomeActivity.this, RouterHub.APP_MAINACTIVITY);
                        finish();
                    }
                });
    }

    private void startAnim() {

        ObjectAnimator animatorX = ObjectAnimator.ofFloat(mIVEntry, "scaleX", 1f, SCALE_END);
        ObjectAnimator animatorY = ObjectAnimator.ofFloat(mIVEntry, "scaleY", 1f, SCALE_END);

        AnimatorSet set = new AnimatorSet();
        set.setDuration(ANIM_TIME).play(animatorX).with(animatorY);
        set.start();

        set.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationEnd(Animator animation) {
//                if (BuildConfig.IS_SALALS_CLIENT) {
//                    Utils.navigation(WelcomeActivity.this, RouterHub.SELLER_CLIENT_LOGIN_LOGIN_ACTIVITY);
//                } else {
//                    Utils.navigation(WelcomeActivity.this, RouterHub.SELLER_CLIENT_LOGIN_LOGIN_ACTIVITY);
//                }
                finish();
            }
        });
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void messageOk() {
        startMainActivity();
    }

    @Override
    public void showMessage(@NonNull String message) {

    }

    @Override
    public void homepageInformsSuccess(HomepageInforms homepageInforms) {

        if (homepageInforms != null && homepageInforms.getData() != null && homepageInforms.getData().size() > 0) {

            new MainPopup(this, homepageInforms.getData().get(0).getTitle(), this).showPopupWindow();

        }
    }
}
